package language;

import java.util.Locale;
import java.util.ResourceBundle;

import com.sharkysoft.printf.Printf;
import com.sharkysoft.printf.PrintfTemplate;

public class TextHandler {
	
	private ResourceBundle bundle;
	
	private Languages actualLanguage = Languages.de_DE;
	
	private boolean isLoaded = false;
	
	private void load() {
		switch (this.actualLanguage) {
		case de_DE:
			bundle = ResourceBundle.getBundle("Messages", Locale.GERMANY);
			break;
		case en_GB:
			bundle = ResourceBundle.getBundle("Messages", Locale.UK);
			break;
		default:
			bundle = ResourceBundle.getBundle("Messages", Locale.GERMANY);
			break;
		}
	}
	
	private void checkIfLoaded() {
		if (!isLoaded) {
			isLoaded = true;
			load();
		}
	}
	
	public String printf(String key, Object[] args) {
		checkIfLoaded();
		if (bundle.containsKey(key)) {
			if (args == null || args.length == 0) {
				return Printf.format(bundle.getString(key));
			} else {
				PrintfTemplate template = new PrintfTemplate(bundle.getString(key));
				return Printf.format(template, args);
			}
		} else {
			return key;
		}
	}
	
	public String printf(String key) {
		return this.printf(key, null);
	}
	
	public void setLanguage(Languages actualLang) {
		this.actualLanguage = actualLang;
		isLoaded = false;
	}
	
	public Languages getActualLanguage() {
		return actualLanguage;
	}

}
